#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "customglwidget.h"

#include <QMessageBox>
#include <QGLWidget>

// variables mainly controlling drawing
const int windowSize = 500; // max size of bar graphs
int yH = 55; // upper y
int yL = 555; // lower y
int xL = 100; // left x
int xR = 350; // right x
int ll = 0; // used for insensity of left bar red colour
int rr = 0; // used for insensity of right bar red colour
int lefty = 250; //Nm measurements from devices
int righty = 250;
int leftOffset = 250; // ratio of slider and Nm values
int rightOffset = 250;
int lefttrue = lefty * leftOffset; // drawing, length of bars
int righttrue = righty * rightOffset;
bool status = false; // if device is connected
QString displayString = "Device not loaded";


/**********************
 * Constructor
 */
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //intialise NGIO device, and try load device
    ngios = new Ngio();
    ngios->FirstStep();
    status = ngios->getStatus(); // loaded successfully?

    ui->setupUi(this);

    //initate timer that controls readings update, and painting
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(updateSlot()));
    timer->start(20);

    // set some painting variables
    scene = new CScene();
    view = new QGraphicsView(this);
    view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    view->setScene(scene);
    setCentralWidget(view);
}
// end Constructor


/**********************
 * Destructor
 */
MainWindow::~MainWindow()
{
    ngios->ThirdStep(); //destroy device reader properly
    timer->stop();
    delete view;
    delete scene;
    delete ui;
}
// end destructor


/**********************
 * Creates new 'Controls' window when menu item is triggered
 */
void MainWindow::on_actionNew_Window_triggered()
{
    mDialog = new MyDialog(this);
    mDialog->show();
}
// end on_actionNew_Window_triggered()


/**********************
 * Slot for when timer fires, getting readings from dynamometers, and
 * the control window, and repaints the charts
 */
void MainWindow::updateSlot()
{
    // perform mesaurement
    ngios->SecondStep();
    lefty = ngios->getLeft();
    righty = ngios->getRight();

    //get slider info and work out Nm/slider ratio
    leftOffset = windowSize/mDialog->getLeft();
    rightOffset = windowSize/mDialog->getRight();

    //work out graphing measurements
    lefttrue = lefty * (leftOffset > 0 ? leftOffset : 0);
    righttrue = righty * (rightOffset > 0 ? rightOffset: 0);

    scene->ChangeChildValues(((double)lefttrue/windowSize)*100, ((double)righttrue/windowSize)*100);

}
// end updateSlot()


/**********************
 * Paints the bar graphs representing dynamometer strenghts.
 */
void MainWindow::paintEvent(QPaintEvent *e)
{

}
// paintEvent(QPaintEvent *e)


/**********************
 * Slot for 'Close' menu item that closes the program when it is pressed
 */
void MainWindow::on_actionClose_triggered()
{
    MainWindow::close();
}
// end on_actionClose_triggered()


/**********************
 * Slot for 'Reload device' that reloads the device if it has not successfully loaded.
 */
void MainWindow::on_actionReload_Device_triggered()
{
    if(status == false)
    { // reload device
        ngios = new Ngio();
        ngios->FirstStep();
        status = ngios->getStatus();
    }
    else
    {
        // if device is already loaded, do not reload but display alert
        QMessageBox msgBox;
        msgBox.setWindowTitle("Hello");
        msgBox.setIcon(QMessageBox::NoIcon);
        msgBox.setText("Device is already loaded");
        msgBox.exec();
    }
}
// end on_actionReload_Device_triggered()
